{$R-}

(*
	SPortHasChar(port number) -- Return "true" if the serial port has data; "false" otherwise.
	By Harry Chesley.  DO NOT call the author!  Contact Apple Developer 
	Support on AppleLink "MacDTS" or on MCI "MacTech".

	ŠApple Computer, Inc. 1987
	All Rights Reserved.

	To compile and link this file using Macintosh Programmer's Workshop,

	pascal -w portHasChar.p
	link -m ENTRYPOINT -o HyperTerm -rt XFCN=0 -sn Main=SPortHasChar portHasChar.p.o "{MPW}"Libraries:interface.o
	
*)

{$S SPortHasChar } 	{ Segment name must be the same as the command name. }

unit DummyUnit;

interface

uses MemTypes, QuickDraw, OSIntf, HyperXCmd;

procedure EntryPoint(paramPtr: XCmdPtr);
	
implementation

type

Str31 = String[31];

procedure SPortHasChar(paramPtr: XCmdPtr); forward;

procedure EntryPoint(paramPtr: XCmdPtr);

	begin
		SPortHasChar(paramPtr);
	end;

procedure SPortHasChar(paramPtr: XCmdPtr);

	var portNumber: integer;
		inPort: integer;
		str: Str255;
		l: longInt;

	{$I XCmdGlue.inc}

	procedure Fail(errMsg: Str255); { set theResult and quit }
		begin
			paramPtr^.returnValue := PasToZero(errMsg);
			exit(SPortHasChar);
		end;

	begin
		if paramPtr^.paramCount <> 1 then Fail('parameter count is not 1');

		ZeroToPas(paramPtr^.params[1]^,str);		{ First parameter is port number. }
		portNumber := StrToNum(str);
		if (portNumber < 1) or (portNumber > 2) then Fail('invalid port number');

		if portNumber = 1 then inPort := -6
		else inPort := -8;
		if SerGetBuf(inPort,l) <> noErr then Fail('SerGetBuf failed');
		if l = 0 then paramPtr^.returnValue := PasToZero('false')
		else paramPtr^.returnValue := PasToZero('true');
	end;

end.
